package com.example.e_comm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.e_comm.entity.User;
import com.example.e_comm.mapper.UserMapper;
import com.example.e_comm.service.UserService;
import com.example.e_comm.util.RedisUtil;
import com.example.e_comm.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    @Autowired
    private RedisUtil redisUtil;
    
    @Autowired
    private TokenUtil tokenUtil;

    @Override
    public boolean register(User user) {
        // 查询 username, email 是否已存在
        if (this.count(new QueryWrapper<User>()
                .eq("username", user.getUsername())) > 0) {
            throw new IllegalArgumentException("用户名已存在，请替换其它用户名！");
        }

        if (this.count(new QueryWrapper<User>()
                .eq("email", user.getEmail())) > 0) {
            throw new IllegalArgumentException("邮箱已被注册！");
        }
        if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
            throw new IllegalArgumentException("密码不能为空！");
        }

        // 对密码进行MD5加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setStatus(1);

        return this.save(user);
    }

    @Override
    public String login(String username, String password) {
        User user = this.getOne(new QueryWrapper<User>()
                .eq("username", username)
                .eq("password", DigestUtils.md5DigestAsHex(password.getBytes()))
                .eq("status", 1));
        if (user != null) {
            String token = tokenUtil.generateToken(user.getId());
            redisUtil.set(token, user.getId());
            return token;
        }
        return null;
    }

    @Override
    public String getUserInfo(String token) {
        Object obj = redisUtil.get(token);
        if (obj != null) {
            Long userId = (Long) obj;
            User user = this.getById(userId);
            if (user != null) {
                return user.toString();
            }
        }
        return null;
    }

    @Override
    public String updateUserInfo(String token, User user) {
        Object obj = redisUtil.get(token);
        if (obj != null) {
            Long userId = (Long) obj;
            user.setId(userId);
            this.updateById(user);
            return "用户信息更新成功";
        }
        return null;
    }

    @Override
    public String updateUserPassword(String token, String oldPassword, String newPassword) {
        Object obj = redisUtil.get(token);
        if (obj != null) {
            Long userId = (Long) obj;
            User user = this.getById(userId);
            if (user.getPassword().equals(DigestUtils.md5DigestAsHex(oldPassword.getBytes()))) {
                user.setPassword(DigestUtils.md5DigestAsHex(newPassword.getBytes()));
                this.updateById(user);
                return "密码更新成功";
            }
        }
        return null;
    }
}